#include <sys/stat.h>
#include "AdvDSProfile.hpp"
#include "Notification.hpp"
#include "States/DefaultState.hpp"
#include "States/DialogState.hpp"
#ifndef EMULATION
#include <3ds.h>
#endif

using namespace cpp3ds;
using namespace TweenEngine;

namespace AdvDSProfile {

AdvDSProfile::AdvDSProfile()
: Game(0x100000)
{
	m_stateStack = new StateStack(State::Context(m_text, m_data));
	m_stateStack->registerState<DefaultState>(States::Default);
	m_stateStack->registerState<DialogState>(States::Dialog);

	m_stateStack->pushState(States::Default);

	textFPS.setFillColor(cpp3ds::Color::Red);
	textFPS.setCharacterSize(20);
}

AdvDSProfile::~AdvDSProfile()
{
	delete m_stateStack;
}

void AdvDSProfile::update(float delta)
{
	// Need to update before checking if empty
	m_stateStack->update(delta);
	if (m_stateStack->isEmpty()) {
		exit();
	}

	Notification::update(delta);

#ifndef NDEBUG
	static int i;
	if (i++ % 10 == 0) {
		textFPS.setString(_("%.1f fps", 1.f / delta));
		textFPS.setPosition(395 - textFPS.getGlobalBounds().width, 2.f);
	}
#endif
}

void AdvDSProfile::processEvent(Event& event)
{
	m_stateStack->processEvent(event);
}

void AdvDSProfile::renderTopScreen(Window& window)
{
	window.clear(Color::White);

	m_stateStack->renderTopScreen(window);
	for (auto& notification : Notification::notifications)
		window.draw(*notification);

#ifndef NDEBUG
	window.draw(textFPS);
#endif
}

void AdvDSProfile::renderBottomScreen(Window& window)
{
	window.clear(Color::White);

	m_stateStack->renderBottomScreen(window);
}

} // namespace AdvDSProfile
